package com.smec.remes.module.request.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hwangjr.rxbus.Bus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewRequestBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.ImageDialog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/7.
 */

public class SmecRequestAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int  NULLDATA = 0;
    private static int  DATA = 1;
    private ArrayList<AbleMatnr> ableMatnrArrayList=new ArrayList<>();
    private ImageDialog imageDialog;
    private Context mcontext;
    private Bus mRxbus;

    public SmecRequestAdapter(Context context,Bus smecBus) {
        this.mcontext=context;
        this.mRxbus=smecBus;
    }

    public void setArrayList(ArrayList<AbleMatnr> list){
        if(!CommonUtils.notEmpty(list)){
            return;
        }
        if(!CommonUtils.notEmpty(ableMatnrArrayList)){
            ableMatnrArrayList=list;
            notifyDataSetChanged();
            return;
        }
        for(int i=ableMatnrArrayList.size()-1;i>=0;i--){
            String matnr1=ableMatnrArrayList.get(i).getMatnr();
            for(int j=0;j<list.size();j++){
                String matnr2=list.get(j).getMatnr();
                if(matnr1.equals(matnr2)){
                    ableMatnrArrayList.remove(i);
                }
            }
        }
        ableMatnrArrayList.addAll(list);
        notifyDataSetChanged();
    }

    public void _clearList(){
        ableMatnrArrayList.clear();
        notifyDataSetChanged();
    }

    public ArrayList<AbleMatnr> getAbleMatnrArrayList() {
        return ableMatnrArrayList;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA){
            LayoutRecyclerviewRequestBinding layoutRecyclerviewRequestBinding=DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_request,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewRequestBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewRequestBinding);
            return smecBaseRecyclerViewHolder;
        }else{
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
    }


    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        if(holder.getViewType() == DATA) {
            final LayoutRecyclerviewRequestBinding layoutRecyclerviewRequestBinding = (LayoutRecyclerviewRequestBinding) holder.getBinding();
            String all=ableMatnrArrayList.get(position).getPriceALL()+" RMB";
            layoutRecyclerviewRequestBinding.tvPriceAll.setText(all);
            layoutRecyclerviewRequestBinding.setVariable(BR.search, ableMatnrArrayList.get(position));
            layoutRecyclerviewRequestBinding.ivItemreduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   if (CommonUtils.notEmpty(ableMatnrArrayList)){
                       ableMatnrArrayList.remove(position);
                       if(CommonUtils.notEmpty(ableMatnrArrayList)){
                           mRxbus.post("REDUCE",ableMatnrArrayList);
                       }else {
                           mRxbus.post("REDUCE_NULL","aa");
                       }
                       notifyDataSetChanged();
                   }
                }
            });
            if (TextUtils.isEmpty(ableMatnrArrayList.get(position).getChangeMessage())) {
                layoutRecyclerviewRequestBinding.change.setVisibility(View.GONE);
            }
            if(TextUtils.isEmpty(ableMatnrArrayList.get(position).getImageLink())){
                layoutRecyclerviewRequestBinding.llBiamap.setVisibility(View.GONE);
            }else {
                layoutRecyclerviewRequestBinding.llBitmapurl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(ableMatnrArrayList.get(position).getImageLink())) {
                            imageDialog = new ImageDialog(mcontext, R.style.project_back);
                            ImageView imageView = (ImageView) imageDialog.getCustomView().findViewById(R.id.iv_image);
                            Glide.with(mcontext)
                                    .load(ableMatnrArrayList.get(position).getImageLink())
                                    .error(R.drawable.smec_glide_error)
                                    .into(imageView);
                            imageDialog.show();

                        } else {
                            Toast.makeText(mcontext, "没有图片", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
        holder.getBinding().executePendingBindings();//刷新界面
    }

    @Override
    public int getItemViewType(int position) {
        if(ableMatnrArrayList == null || ableMatnrArrayList.size() == 0){
            return NULLDATA;
        }else{
            return DATA;
        }
    }

    @Override
    public int getItemCount() {
        if(ableMatnrArrayList.size() == 0){
            return 1;
        }else{
            return ableMatnrArrayList.size();
        }
    }
}
